package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.db.PageResult;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountVo;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
    CustomerReservationService customerReservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        Integer count = customerReservationService.cancelledCount(userId);
        return ResponseResult.success(count);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult countByTime(@RequestParam Long time) {
        List<CountVo> countVo = customerReservationService.countByTime(LocalDateTimeUtil.of(time));
        return ResponseResult.success(countVo);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult add(@RequestBody ReservationDto reservationDto) {
        customerReservationService.add(reservationDto);
        return ResponseResult.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public ResponseResult<PageResponse<ReservationVo>> page(@RequestParam(defaultValue = "1") Integer pageNum,
                                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                                            Integer status) {
        PageResponse<ReservationVo> pageResult =customerReservationService.page(pageNum,pageSize,status);
        return ResponseResult.success(pageResult);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消")
    public ResponseResult cancel(@PathVariable Long id){
        customerReservationService.cancel(id);
        return ResponseResult.success();
    }

}